package com.wgchao.authmanager.application.controller;

import com.wgchao.authmanager.application.filter.AuthMethod;
import com.wgchao.authmanager.application.filter.Authentication;
import com.wgchao.authmanager.application.service.RoleService;
import com.wgchao.authmanager.application.vo.RoleVo;
import com.wgchao.authmanager.domain.params.RoleCreator;
import com.wgchao.authmanager.domain.params.RoleUpdator;
import com.wgchao.authmanager.infrastructure.resp.BackResult;
import com.wgchao.authmanager.infrastructure.resp.PageResult;
import com.wgchao.authmanager.query.RoleInfoQuery;
import com.wgchao.authmanager.query.specification.RoleCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: RoleManagerController
 * @author: wgchao
 * @createTime: 2020/5/7 3:26 PM
 * @Description: 角色管理
 */
@RestController
@RequestMapping("/aum/front/role")
@Api(value = "/aum/front/role", description = "角色管理")
public class RoleManagerController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleInfoQuery roleInfoQuery;

    @PostMapping("/{agencyCode}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "向机构添加角色", notes = "需要身份验证")
    public BackResult createAdminToRole(@ApiParam(name = "agencyCode", required = true, value = "机构编号（url参数）")
                                          @PathVariable String agencyCode,
                                        @ApiParam(name = "creator", required = true, value = "角色信息")
                                          @RequestBody RoleCreator creator){
        roleService.addRole(agencyCode, creator, "admin");
        return BackResult.success();
    }

    @PutMapping("/resive/{agencyCode}/{roleName}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "修改角色信息", notes = "需要身份验证")
    public BackResult resiveAdminInfo(@ApiParam(value = "机构编号", name = "agencyCode", required = true)
                                      @PathVariable String agencyCode,
                                      @ApiParam(value = "角色名称", name = "roleName", required = true)
                                      @PathVariable String roleName,
                                      @ApiParam(value = "账角色修改信息", name = "updator", required = true)
                                      @RequestBody RoleUpdator updator){
        roleService.resiveRoleInfo(agencyCode, roleName, updator, "admin");
        return BackResult.success();
    }

    @DeleteMapping("/remove/{agencyCode}/{roleName}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "删除角色", notes = "需要身份验证")
    public BackResult removeAdmin(@ApiParam(value = "机构编号", name = "agencyCode", required = true)
                                      @PathVariable String agencyCode,
                                  @ApiParam(value = "角色名称", name = "roleName", required = true)
                                      @PathVariable String roleName){
        roleService.removeRole(agencyCode, roleName, "admin");
        return BackResult.success();
    }

    @PutMapping("/stop/{agencyCode}/{roleName}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "停用账户", notes = "需要身份验证")
    public BackResult stopUsingAdmin(@ApiParam(value = "机构编号", name = "agencyCode", required = true)
                                         @PathVariable String agencyCode,
                                     @ApiParam(value = "角色名称", name = "roleName", required = true)
                                         @PathVariable String roleName){
        roleService.stopUsingRole(agencyCode, roleName, "admin");
        return BackResult.success();
    }

    @PutMapping("/reuse/{agencyCode}/{roleName}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "重新启用管理员账户", notes = "需要身份验证")
    public BackResult reUseAdmin(@ApiParam(value = "机构编号", name = "agencyCode", required = true)
                                     @PathVariable String agencyCode,
                                 @ApiParam(value = "角色名称", name = "roleName", required = true)
                                     @PathVariable String roleName){
        roleService.reUseRole(agencyCode, roleName, "admin");
        return BackResult.success();
    }

    @GetMapping("/page/condition")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "条件分页查询角色信息", notes = "需要身份验证")
    public BackResult<PageResult<RoleVo>> searchRolesOfCri(RoleCriteria criteria) throws Exception{
        PageResult<RoleVo> roleVoPageResult = roleInfoQuery.searchRolesOfCri(criteria, "admin");
        return BackResult.success().setData(roleVoPageResult);
    }

    @GetMapping("/agency/canuse")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "查询机构下可用的角色", notes = "需要身份验证")
    public BackResult<List<RoleVo>> searchRolesOfCanUseWithAgency(@ApiParam(value = "机构编号", name = "agencyCode", required = true)
                                                                  @PathVariable String agencyCode){
        List<RoleVo> roleVos = roleInfoQuery.searchRolesOfCanUseWithAgency(agencyCode);
        return BackResult.success().setData(roleVos);
    }

}
